/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

#include <stdio.h>
#include "stm32l4xx_hal.h"

#include "cpost.h"
#include "cevent.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
    .name = "defaultTask",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};

osThreadId_t cpost_task_handle;
const osThreadAttr_t cpost_task_attributes = {
    .name = "cpost_task",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityAboveNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void cpost_task(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
 * @brief  FreeRTOS initialization
 * @param  None
 * @retval None
 */
void MX_FREERTOS_Init(void)
{
    /* Create the thread(s) */
    /* creation of cpost task */
    cpost_task_handle = osThreadNew(cpost_task, NULL, &cpost_task_attributes);
    /* creation of defaultTask */
    defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

    /* USER CODE BEGIN RTOS_THREADS */
    /* add threads, ... */
    /* USER CODE END RTOS_THREADS */

    /* USER CODE BEGIN RTOS_EVENTS */
    /* add events, ... */
    /* USER CODE END RTOS_EVENTS */
}

void cpost_task(void *argument)
{
    printf("-------------cpost task init.---------------\r\n");

    ceventInit();
    ceventPost(EVENT_INIT_STAGE1);
    ceventPost(EVENT_INIT_STAGE2);
    ceventPost(EVENT_INIT_STAGE3);

    printf("-------------cpost task init done.---------------\r\n");

    while(1) {
        cpostProcess();
        osDelay(10);
    }
}

void cevent_test_print(void)
{
    printf("cevent init stage1.\r\n");
}
CEVENT_EXPORT(EVENT_INIT_STAGE1, cevent_test_print);
void cevent_test_print_2(void)
{
    printf("cevent init stage2.\r\n");
}
CEVENT_EXPORT(EVENT_INIT_STAGE2, cevent_test_print_2);
void cevent_test_print_3(void)
{
    printf("cevent init stage3.\r\n");
}
CEVENT_EXPORT(EVENT_INIT_STAGE3, cevent_test_print_3);

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief  Function implementing the defaultTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
    /* USER CODE BEGIN StartDefaultTask */
    //printf("start default os task, cpu tick:%d.\r\n", configCPU_CLOCK_HZ);
    //printf("default task init, get hal tick:%d, get os tick:%d.\r\n", HAL_GetTick(), osKernelSysTick());
    /* Infinite loop */
    for (;;)
    {
        osDelay(1000);
        // printf("default task loop, get hal tick:%d, get os tick:%d.\r\n", HAL_GetTick(), osKernelSysTick());
    }
    /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
